package c2.advanced.share;

import c2.advanced.server.configuration.SetupContext;
import cate.common.table.EContext;
import cate.common.table.EExcel;
import cate.common.table.activity.ActivityExcel;
import cate.common.table.activity.crossrankactivity.CrossRankActivityExcel;
import cate.common.table.activity.tbboss.TBBossExcel;
import cate.common.table.activity.throne.TBExcel;
import cate.common.table.attr.AttrExcel;
import cate.common.table.camp.CampAttrExcel;
import cate.common.table.common.CommonExcel;
import cate.common.table.cross_boss.CrossBossFarmExcel;
import cate.common.table.dynins.DynInstanceExcel;
import cate.common.table.fight.buff.BuffExcel;
import cate.common.table.fight.destiny.DestinyExcel;
import cate.common.table.fight.scene.FightSceneExcel;
import cate.common.table.fight.skill.SkillExcel;
import cate.common.table.hallow.HallowExcel;
import cate.common.table.ins.InstanceExcel;
import cate.common.table.item.ItemExcel;
import cate.common.table.item.awakenfairy.AwakenFairyExcel;
import cate.common.table.item.equip.EquipExcel;
import cate.common.table.item.fairy.FairyExcel;
import cate.common.table.item.furniture.FurnitureExcel;
import cate.common.table.item.gem.GemExcel;
import cate.common.table.item.hero.HeroExcel;
import cate.common.table.item.inscription.InscriptionExcel;
import cate.common.table.item.kitchenware.KitchenwareExcel;
import cate.common.table.item.rune.RuneExcel;
import cate.common.table.item.spice.SpiceExcel;
import cate.common.table.item.staruptask.StarUpTaskExcel;
import cate.common.table.item.yoke.YokeExcel;
import cate.common.table.jungle.JungleExcel;
import cate.common.table.pvp.arena.ArenaExcel;
import cate.common.table.pvp.championrace.ChampionRaceExcel;
import cate.common.table.pvp.extra.PvpRankPointExtraExcel;
import cate.common.table.pvp.gradepvp.GradePvpExcel;
import cate.common.table.pvp.guildwar.GuildWarExcel;
import cate.common.table.pvp.ladder.LadderExcel;
import cate.common.table.pvp.minecraft.MinecraftExcel;
import cate.common.table.pvp.teampvp.TeamPvpExcel;
import cate.common.table.res.achievement.AchievementExcel;
import cate.common.table.res.drop.DropExcel;
import cate.common.table.res.hero.back.HeroBackExcel;
import cate.common.table.res.holy_pet.HolyPetExcel;
import cate.game.table.GameTableContext;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TableFacade {

    public ItemExcel item = new ItemExcel();
    public HeroExcel hero = new HeroExcel();
    public EquipExcel equip = new EquipExcel();
    public SpiceExcel spice = new SpiceExcel();
    public DynInstanceExcel dynIns = new DynInstanceExcel();
    public InstanceExcel ins = new InstanceExcel();
    public FairyExcel fairy = new FairyExcel();
    public RuneExcel rune = new RuneExcel();
    public GemExcel gem = new GemExcel();
    public HallowExcel hallow = new HallowExcel();
    public FurnitureExcel furniture = new FurnitureExcel();
    public SkillExcel skill = new SkillExcel();
    public BuffExcel buff = new BuffExcel();
    public CampAttrExcel campAttr = new CampAttrExcel();
    public FightSceneExcel scene = new FightSceneExcel();
    public DestinyExcel destiny = new DestinyExcel();
    public HeroBackExcel heroBack = new HeroBackExcel();
    public StarUpTaskExcel starUpTask = new StarUpTaskExcel();
    public AttrExcel attr = new AttrExcel();
    public LadderExcel ladder = new LadderExcel();
    public CommonExcel common = new CommonExcel();
    public MinecraftExcel minecraft = new MinecraftExcel();
    public DropExcel drop = new DropExcel();
    public InscriptionExcel inscription = new InscriptionExcel();
    public YokeExcel yoke = new YokeExcel();
    public ArenaExcel arena = new ArenaExcel();
    public ActivityExcel activity = new ActivityExcel();
    public CrossRankActivityExcel crossRankAct = new CrossRankActivityExcel();
    public AchievementExcel achievement = new AchievementExcel();
    public CrossBossFarmExcel crossBossFarm = new CrossBossFarmExcel();
    public GuildWarExcel guildWar = new GuildWarExcel();
    public ChampionRaceExcel championRace = new ChampionRaceExcel();
    public AwakenFairyExcel awakenFairy = new AwakenFairyExcel();
    public KitchenwareExcel kitchenware = new KitchenwareExcel();
    public JungleExcel jungle = new JungleExcel();
    public TBExcel throne = new TBExcel();
    public TBBossExcel tbBoss = new TBBossExcel();
    public GradePvpExcel gradePvp = new GradePvpExcel();
    public TeamPvpExcel teamPvp = new TeamPvpExcel();
    public HolyPetExcel holyPet = new HolyPetExcel();
    public PvpRankPointExtraExcel pvpRankPointExtra = new PvpRankPointExtraExcel();

    private EExcel[] getExcels() {
        List<EExcel> excelList = new ArrayList<>();
        Field[] fields = getClass().getDeclaredFields();
        for (Field field : fields) {
            if (EExcel.class.isAssignableFrom(field.getType())) {
                try{
                    excelList.add((EExcel) field.get(this));
                } catch (Exception e) {
                    log.error("table facade读表异常辣， 表：{}", field.getType());
                }
            }
        }
        EExcel[] excels = new EExcel[excelList.size()];
        for (int i = 0; i < excelList.size(); ++ i) {
            excels[i] = excelList.get(i);
        }
        return excels;
    }

    public void setup(SetupContext context) {
        EContext ctx = new GameTableContext(context.env.getSelf().getExcelPath(), null);
        for (EExcel excel : getExcels()) {
            excel.setContext(ctx);
            excel.load();
        }
        for (EExcel excel : getExcels()) {
            excel.decode();
        }
    }
}
